<?php

namespace app\common\library;

use think\Exception;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\Json;


class Express100
{

    const KEY ='HemSnfhy3812';
    const CUSTOMER ='550CC982F94D5F4CF15FECAE93258198';

//    const KEY ='hNKNHSds9868';
//    const CUSTOMER ='6D44A7BC32C51FBF944F115312164925';

    const  URL='https://poll.kuaidi100.com/poll/query.do';

    /*
     * 通过快递单号 查物流信息
     */
    public static function getKuaidiInfo($no = "")
    {
        $url = "http://m.kuaidi100.com/autonumber/auto?num=3961260312889";
        $post = "http://m.kuaidi100.com/query?type=yunda&postid=3961260312889";
    }
    /*
     * 快递信息
     */
    public static function getExpressInfo($expressNo,$mobile='')
    {
        $url = "http://m.kuaidi100.com/autonumber/auto?num=".$expressNo."&key=".self::KEY;
        $dataInfo = json_decode(self::curl($url),true);
        $eg = <<<of
Array
(
    [0] => Array
        (
            [lengthPre] => 15
            [comCode] => shentong
            [name] => 申通快递
        )
)

Array
(
    [returnCode] => 601
    [message] => key过期
    [result] => 
)
of;

//        p($dataInfo);die;
        //$comCode = "comCode";
        if(empty($dataInfo[0]['comCode'])){
            throw new Exception($dataInfo['message']??"查询失败");
        }
        if("shunfeng" == $dataInfo[0]['comCode']){

        }


        $json=json_encode(['com'=>$dataInfo[0]['comCode'],'num'=>$expressNo,'phone' =>$mobile, 'from' => '','to' => '','resultv2' => '1'],true);
        $post_data=[
        'customer'=>self::CUSTOMER,
            'param'=>$json
        ];
        $post_data['sign']=strtoupper(md5($json.self::KEY.self::CUSTOMER));
        $params = "";
        foreach ($post_data as $k=>$v) {
            $params .= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }
        $post_data = substr($params, 0, -1);
        $data= self::curl_post($post_data);
        return $data;
    }


    public static function curl_post($post_data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,self::URL);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        $data = str_replace("\"", '"', $result );
        $data = json_decode($data,true);
        return $data;
    }
    public static function curl($url)
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "",
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
                "postman-token: 119909ff-7f0b-42e0-fee4-cca172decd6f"
            ),
        ));
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        if ($err) {
            return "cURL Error #:" . $err;
        } else {
            return $response;
        }
    }






}
